package com.chuangjiangx.member.coupon.ddd.query;

import com.chuangjiangx.member.coupon.ddd.dal.dto.*;
import com.chuangjiangx.member.coupon.ddd.dal.mapper.MbrCouponMarketingMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.NumberFormat;
import java.util.List;

/**
 * 卡劵营销查询
 * @author cj-yzh
 * @date 2018/8/10 下午4:21
 *
 */
@Component
public class MbrCouponMarketingQuery {

    @Autowired
    private MbrCouponMarketingMapper couponMarketingMapper;

    //查询卡劵的基本信息，如消费情况 使用情况
    public MbrCouponMarketingInfo getGerneralInfo(long marchantId, String name){
        MbrCouponMarketingInfo mbrCouponMarketingInfo = couponMarketingMapper.selectCoupon(marchantId, name);
        if(mbrCouponMarketingInfo == null){
            return null;
        }
        //ok

        //查询信息
        MbrCouponMarketingInfo mbrCouponMarketingInfo1 = couponMarketingMapper.selectCouponByMerchantId(marchantId, name);

        if(mbrCouponMarketingInfo1 != null){
            mbrCouponMarketingInfo.setCouponId(mbrCouponMarketingInfo1.getCouponId());
            mbrCouponMarketingInfo.setOrderTotalMoney(mbrCouponMarketingInfo1.getOrderTotalMoney());
            mbrCouponMarketingInfo.setPreferentialMoney(mbrCouponMarketingInfo1.getPreferentialMoney());
        }
        MbrUsedInfo usedInfo = couponMarketingMapper.selectCouponUsed(marchantId, name);
        //设置验卡劵张数 和转换率
        if(usedInfo != null){
            mbrCouponMarketingInfo.setUsedNum(usedInfo.getUsedNum());
        }

        // 创建一个数值格式化对象
        NumberFormat nt = NumberFormat.getInstance();
        // 设置精确到小数点后2位
        nt.setMinimumFractionDigits(2);
        nt.setMaximumFractionDigits(2);
        long usedNum = mbrCouponMarketingInfo.getUsedNum();
        long receiveNum = mbrCouponMarketingInfo.getReceiveNum();
        if(usedNum != 0 && receiveNum != 0 ){
            String conversion = nt.format((double) usedNum / receiveNum);
            mbrCouponMarketingInfo.setConversion( format(conversion));

        }
        else {
            mbrCouponMarketingInfo.setConversion("00.00");
        }

        return mbrCouponMarketingInfo;
    }

    //按天查询卡劵的领取情况
    public List<MbrReceiveByDay> receiveCouponByDay(long marchantId, String name){
        List<MbrReceiveByDay> byDay = couponMarketingMapper.receiveCouponByDay(marchantId, name);
        return byDay;
    }

    //活动拉新效果
    public MbrActivityPullNewEffect activityPullNewEffect(long marchantId, String name){
        //拉新人数
        MbrActivityPullNewEffect pullNewNum = couponMarketingMapper.selectNewNum(marchantId,name);
        int newNum = 0;
        if(pullNewNum != null){
            newNum = pullNewNum.getNewNum();
        }
        //回头客人数
        MbrActivityPullNewEffect pullRepeatCustomers = couponMarketingMapper.selectRepeatCustomers(marchantId ,name);
        int repeatCustomersNum = 0;
        if(pullRepeatCustomers != null){
            repeatCustomersNum = pullRepeatCustomers.getRepeatCustomersNum();
        }
        //查询挽回丢失人数
        MbrActivityPullNewEffect pullSaveLoseNum = couponMarketingMapper.selectSaveLoseNum(marchantId,name);
        int saveLoseNum = 0;
        if(pullSaveLoseNum != null){
            saveLoseNum = pullSaveLoseNum.getSaveLoseNum();
        }
        //z总人数
        int totalNum = newNum+repeatCustomersNum+saveLoseNum;
        MbrActivityPullNewEffect activityPullNewEffect = new MbrActivityPullNewEffect();
        activityPullNewEffect.setNewNum(newNum);
        activityPullNewEffect.setRepeatCustomersNum(repeatCustomersNum);
        activityPullNewEffect.setSaveLoseNum((saveLoseNum));
        // 创建一个数值格式化对象
        NumberFormat nt = NumberFormat.getInstance();
        // 设置精确到小数点后2位
        nt.setMinimumFractionDigits(2);
        nt.setMaximumFractionDigits(2);
        if(totalNum == 0){
            activityPullNewEffect.setNewAccountedFor("00.00");
            activityPullNewEffect.setRepeatAccountedFor("00.00");
            activityPullNewEffect.setSaveAccountedFor("00.00");
        }
        //拉新
        if(newNum != 0 ){
            String accountedFor = nt.format((double) newNum / totalNum);
            activityPullNewEffect.setNewAccountedFor(format(accountedFor));
        }
        //设置回头客占比
        if(saveLoseNum != 0 ){
            String accountedFor = nt.format((double) saveLoseNum / totalNum);
            activityPullNewEffect.setRepeatAccountedFor(format(accountedFor));
        }
        //设置挽回人数占比
        if(saveLoseNum != 0 ){
            String accountedFor = nt.format((double) saveLoseNum / totalNum);
            activityPullNewEffect.setSaveAccountedFor(format(accountedFor));
        }


        return activityPullNewEffect;
    }

    //活动留存效果
    public MbrActivityRetention activityRetention(long marchantId, String name){
        //七天人数
        int sevenNum = 0;
        //一个月的人数
        int oneMonthNum = 0;
        //三个月的人数
        int threeMonthNum = 0;

        //查询7天
        MbrActivityRetention retention = couponMarketingMapper.selectactivityRetention(7, marchantId , name);
        if(retention != null){
            sevenNum = retention.getNum();
            retention.setSevenDayNum(sevenNum);
        }
        MbrActivityRetention inMbrActivityRetention = couponMarketingMapper.selectactivityRetention(30, marchantId,name);
        if(inMbrActivityRetention != null){
            oneMonthNum = inMbrActivityRetention.getNum();
            retention.setOneMonthNum(oneMonthNum);
        }
        MbrActivityRetention inMbrActivityRetention1 = couponMarketingMapper.selectactivityRetention(30 * 3, marchantId,name);
        if(inMbrActivityRetention1 != null){
            threeMonthNum = inMbrActivityRetention1.getNum();
            retention.setThreeMonthNum(threeMonthNum);
        }
        int totalNum = sevenNum + oneMonthNum + threeMonthNum;
        // 创建一个数值格式化对象
        NumberFormat nt = NumberFormat.getInstance();
        // 设置精确到小数点后2位
        nt.setMinimumFractionDigits(2);
        nt.setMaximumFractionDigits(2);
        //设置七天占比
        if(totalNum == 0){
            retention.setSevenConversion("00.00");
            retention.setOneMonthConversion("00.00");
            retention.setThreeMonthConversion("00.00");
        }
        if(sevenNum != 0 ){
            String accountedFor = nt.format((double) sevenNum / totalNum);
            retention.setSevenConversion(format(accountedFor));
        }
        //设置回头客占比
        if(oneMonthNum != 0 ){
            String accountedFor = nt.format((double) oneMonthNum / totalNum);
            retention.setOneMonthConversion(format(accountedFor));
        }
        //设置挽回人数占比
        if(threeMonthNum != 0 ){
            String accountedFor = nt.format((double) threeMonthNum / totalNum);
            retention.setThreeMonthConversion(format(accountedFor));
        }

        return retention;
    }

    //格式化百分比   如果是1.00换成100.00
    private String format(String source){
        if(!"1.00".equals(source)){
            return source;
        }else{
            return "100.00";
        }
    }

}
